# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=graphite2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.8
pkgrel=4
pkgdesc="Font rendering capabilities for complex non-Roman writing systems (mingw-w64)"
arch=('any')
url="https://github.com/silnrsi/graphite"
license=('LGPL2.1+')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=("https://github.com/silnrsi/graphite/releases/download/${pkgver}/${_realname}-${pkgver}.tgz"
        "001-graphite2-1.3.8-win64.patch"
        "002-graphite2-1.2.1-pkgconfig.patch"
"003-graphite2-1.3.8-staticbuild.patch")
sha256sums=('9f3f25b3a8495ce0782e77f69075c0dd9b7c054847b9bf9ff130bec38f4c8cc2'
            'bd6e108929110fcbfd69434221974395a4839710432fe58a9aa432e95bdf4354'
            '22bef0d90c0266b3b0eff01859c1e002f84c165d12242c2bfc153f8f98fb272a'
            'a7d17b8de6bec8d383f62c2a61d6eac86390a7287f5ceebf550bc19155a5adf0')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-graphite2-1.3.8-win64.patch
  patch -p1 -i "${srcdir}"/002-graphite2-1.2.1-pkgconfig.patch
  patch -p1 -i "${srcdir}"/003-graphite2-1.3.8-staticbuild.patch
# python2 fixes
  sed -i "s:\/usr\/bin\/python:\/usr\/bin\/python2:" tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp}
}

check() {
  cd "${srcdir}"/build-${CARCH}
  # python2 fixes
  sed -i "s:python:python2:g" tests/CTestTestfile.cmake
  # we also want to use mingw-w64-x86_64 and mingw-w64-i686 versions of python
  mkdir "${srcdir}"/${_realname}-${pkgver}/tests/backup
  cp "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp} \
   ${srcdir}"/${_realname}-${pkgver}/tests/backup"
  sed -i "s:\/usr\/bin\/python2:${MINGW_PREFIX}\/bin\/python2:" \
   "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp}
  ctest || true
  rm "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp}
  cp "${srcdir}"/${_realname}-${pkgver}/tests/backup/* "${srcdir}"/${_realname}-${pkgver}/tests
  rm -rf "${srcdir}"/${_realname}-${pkgver}/tests/backup
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DGRAPHITE2_COMPARE_RENDERER=ON \
      ../${_realname}-${pkgver}

  make
}


package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR=${pkgdir} install

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
